<?php

namespace backend\modules\farmerhouse\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\farmerhouse\FarmerhouseHouse;

/**
 * FarmerhouseHouseSearch represents the model behind the search form about `common\models\farmerhouse\FarmerhouseHouse`.
 */
class FarmerhouseHouseSearch extends FarmerhouseHouse
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'personid', 'farmerid', 'roomnum', 'canfeed', 'caninnum', 'createtime', 'isrecommand', 'status', 'city', 'county', 'town'], 'integer'],
            [['name', 'envdesc', 'accommodation', 'envphoto', 'accommodationphoto', 'detail', 'feedlive', 'address'], 'safe'],
            [['price'], 'number'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = FarmerhouseHouse::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'personid' => $this->personid,
            'farmerid' => $this->farmerid,
            'roomnum' => $this->roomnum,
            'canfeed' => $this->canfeed,
            'caninnum' => $this->caninnum,
            'price' => $this->price,
            'createtime' => $this->createtime,
            'isrecommand' => $this->isrecommand,
            'status' => $this->status,
            'city' => $this->city,
            'county' => $this->county,
            'town' => $this->town,
        ]);

        $query->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'envdesc', $this->envdesc])
            ->andFilterWhere(['like', 'accommodation', $this->accommodation])
            ->andFilterWhere(['like', 'envphoto', $this->envphoto])
            ->andFilterWhere(['like', 'accommodationphoto', $this->accommodationphoto])
            ->andFilterWhere(['like', 'detail', $this->detail])
            ->andFilterWhere(['like', 'feedlive', $this->feedlive])
            ->andFilterWhere(['like', 'address', $this->address]);

        return $dataProvider;
    }
}
